#include <iostream>
#include <memory>

using namespace std;

class B;

class A
{
    private:
    shared_ptr<B> b_ptr;
    public:
    A()
    {
        cout << "A()" << endl;
    }
    ~A()
    {
        cout<<"~A()"<<endl;
    }
    void set_ptr(shared_ptr<B> pb)
    {
        b_ptr = pb;
    }
};

class B
{
    private:
    weak_ptr<A> a_ptr;
    public:
    B()
    {
        cout << "B()" << endl;
    }
    ~B()
    {
        cout<<"~B()"<<endl;
    }
    void set_ptr(shared_ptr<A> pa)
    {
        a_ptr = pa;
    }
};

int main()
{
    auto sa = make_shared<A>();
    auto sb = make_shared<B>();
    // 让这两个对象互相持有对方指针
    sa->set_ptr(sb);
    sb->set_ptr(sa);
}